//
//  OMGoodOneColumnCollectCell.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/22.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodOneColumnCollectCell.h"
#import "OMConsumerGood.h"

#import <YYText.h>

@interface OMGoodOneColumnCollectCell()

@property (nonatomic, weak) OMProduct *product;
@property (nonatomic, strong) UIButton *tagButton;
@end
@implementation OMGoodOneColumnCollectCell
- (void)configureCellWithObj:(OMFakeGoodData *)good
{
    if ([good isKindOfClass:[OMProduct class]]) {
        [self configureCellWithProduct:(id)good];
        return;
    }
    [OMFakeAction asyncLoadLocalImage:good.image inImageView:_iconImageView];
}

- (void)configureCellWithProduct:(OMProduct *)good
{
    _product = good;
    [_iconImageView kt_imageWithURLStr:good.image placeHolder:[UIImage imageNamed:@"homepage-loading-260-260"] withSize:CGSizeMake(self.iconImageView.size.width*3, self.iconImageView.size.height*3)];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString: good.brand?:@""];
        !title.string.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
        [title appendAttributedString:[[NSAttributedString alloc] initWithString:good.name?:@"" attributes:@{NSFontAttributeName:self.titleLabel.font}]];
        NSString *price = _product.price.floatValue == 0 ?@"线上预约":[[NSString stringWithFormat:@"￥%.2f",good.price.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];

        dispatch_async(dispatch_get_main_queue(), ^{
            _brandLabel.attributedText = title;
            _priceLabel.text = price;
            _specLabel.text = good.unit?:@" ";
        });

    });

    if (_product.promotions.count) {
        self.tagButton.hidden = NO;
        [self.tagButton setTitle:_product.promotions.firstObject.name forState:UIControlStateNormal];
    }else {
        self.tagButton.hidden = YES;
    }

}


- (void)configure
{
    [super configure];

    _iconImageView = [UIImageView new];
    [self.contentView addSubview:_iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(7)));
        make.top.equalTo(@(SM_SIZE(7)));
        make.bottom.equalTo(@(SM_SIZE(-7)));
        make.width.equalTo(_iconImageView.mas_height);
    }];

    _brandLabel = [UILabel new];
    _brandLabel.font = SM_CFont(29);
    _brandLabel.textColor = SM_blackColor;
    [self.contentView addSubview:_brandLabel];
    [_brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).offset(SM_SIZE(15));
        make.top.equalTo(_iconImageView.mas_top).offset(SM_SIZE(20));
        make.right.equalTo(@(SM_SIZE(-15)));
    }];


    _titleLabel = [UILabel new];
    _titleLabel.numberOfLines = 1;
    _titleLabel.font = SM_CFont(29);

    _specLabel = [UILabel new];
    _specLabel.textColor = HEXCOLOR(@"#969696");
    _specLabel.font = SM_CFont(24);
    [self.contentView addSubview:_specLabel];
    [_specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_brandLabel.mas_left);
        make.top.equalTo(_brandLabel.mas_bottom).offset(SM_SIZE(10));
    }];


    _priceLabel = [UILabel new];
    _priceLabel.textColor = SM_redColor;
    _priceLabel.font = SM_NFont(26);
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_brandLabel.mas_left);
        make.bottom.equalTo(_iconImageView.mas_bottom).offset(SM_SIZE(-20));

    }];

    _basketButton = [UIButton new];
    [_basketButton setImage:[UIImage imageNamed:@"home_category_plus"] forState:UIControlStateNormal];
    [self.contentView addSubview:_basketButton];
    [_basketButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.centerY.equalTo(_priceLabel.mas_centerY);
    }];
    @weakify(self);
    [[_basketButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self basketButtonTap:x];
    }];

    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = SM_naviShadowColor;
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@(-.5f));
        make.height.equalTo(@0.5f);
    }];

    _tagButton = [UIButton new];
    [_tagButton setTitleColor:SM_whiteColor forState:UIControlStateNormal];
    _tagButton.titleLabel.font = SM_CFont(25);
    _tagButton.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(5), SM_SIZE(10), SM_SIZE(5), SM_SIZE(10));
    _tagButton.backgroundColor = SM_dotRedColor;
    [self.contentView addSubview:_tagButton];
    [_tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_left);
        make.top.equalTo(self.iconImageView.mas_top);
    }];
    _tagButton.hidden = YES;

}

#pragma mark - Actions
- (void)basketButtonTap:(id)sender
{
    if (!_product) {
        return;
    }
    [OMFMDBCartHelper itemAddition:self.product
               withButtonAnimation:sender
                        targetView:KTSystemStorageSingleton.badgeView];
}

+ (CGFloat)cellHeight
{
    return SM_SIZE(200);
}


@end
